package com.eightywork.life.activity;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.service.CpService;
import com.eightywork.life.util.MyHttpException;
import com.umeng.analytics.MobclickAgent;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class PasswordModiActivity extends BaseActivity implements OnClickListener {
	private EditText password, newpassword1, newpassword2;
	private ImageButton returnButton;
	private Button saveButton;
	private AsyncTask<?, ?, ?> task;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_passwordmodi);
		initView();
	}

	@Override
	protected void onDestroy() {
		if (task != null)
			task.cancel(true);
		super.onDestroy();
	}
	
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}

	public void initView() {
		password = (EditText) findViewById(R.id.password);
		newpassword1 = (EditText) findViewById(R.id.newpassword1);
		newpassword2 = (EditText) findViewById(R.id.newpassword2);
		returnButton = (ImageButton) findViewById(R.id.button_left);
		saveButton = (Button) findViewById(R.id.button_save);
		returnButton.setOnClickListener(this);
		saveButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == returnButton) {
			finish();
		} else if (v == saveButton) {
			save();
		}
	}

	private void save() {
		String passwordString = password.getEditableText().toString();
		String password1String = newpassword1.getEditableText().toString();
		String password2String = newpassword2.getEditableText().toString();
		if (passwordString.equals("")) {
			password.setError(Html.fromHtml("<font color=red>请填写当前密码</font>"));
			return;
		}
		if (password1String.equals("")) {
			newpassword1.setError(Html.fromHtml("<font color=red>请填写新密码</font>"));
			return;
		}
		if (!password1String.equals(password2String)) {
			newpassword2.setError(Html.fromHtml("<font color=red>必须和新密码相同</font>"));
			return;
		}
		task = new ModiTask().execute(passwordString, password1String, password2String);
	}
	
	class ModiTask extends AsyncTask<String, String, String> {
		@Override
		protected void onPreExecute() {
			showProgressDialog("请稍后...");
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {
			try {
				CpService.account(params[0], params[1], params[2]);
			} catch (MyHttpException e) {
				return e.getErrorMessage();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			hideProgressDialog();
			if (result == null) {
				finish();
			} else {
				showMessage(result);
			}
			super.onPostExecute(result);
		}
	}
}
